﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;

namespace CommonMvc.Models.Sys
{
    public class Menu : BasicEntityModel
    {
        public Menu(){
            ChilMenus = new List<Menu>();
        }
        /// <summary>
        /// Id
        /// </summary>		     
        public int Id
        {
            get ; 
            set ; 
        }
        /// <summary>
        /// 名称
        /// </summary>		
        [Required]
        [MaxLength(20)]
        public string Name
        {
            get ; 
            set ; 
        }
        /// <summary>
        /// 跳转的url
        /// </summary>		
      
        public string Url
        {
            get ; 
            set ; 
        }
        /// <summary>
        /// 图标
        /// </summary>		
     
        public string Icon
        {
            get; 
            set ; 
        }
        /// <summary>
        /// 权限
        /// </summary>		
       
       // [RegularExpression("[a-zA-Z]*[|]+[a-zA-Z]*")]
        public string ViewPermission
        {
           get ; 
           set ; 
        }
        [NotMapped]
        public String ControllerName {
            get {
                return ViewPermission.Split('|')[0];
            }
        }
        [NotMapped]
        public String ActionName
        {
            get
            {
                return ViewPermission.Split('|')[1];
            }
        }
        /// <summary>
        /// 上级的id
        /// </summary>		

        public int ParentId
        {
            get;
            set; 
        }

        //public virtual Menu Parent { get; set; }
        /// <summary>
        /// 排序
        /// </summary>		
        [Range(0,100,ErrorMessage ="排序只能输入0到100")]
        public int SortNo
        {
            get; 
            set ; 
        }
        /// <summary>
        /// 备注
        /// </summary>		
      
        public string Memo
        {
            get ; 
            set ; 
        }
        /// <summary>
        /// 状态
        /// </summary>		
        [Range(0,1,ErrorMessage = "只能输入0或1")]
        public int State
        {
            get ; 
            set ; 
        }
        [NotMapped]
        public List<Menu> ChilMenus { get; set; }
        /// <summary>
        /// 子菜单
        /// </summary>
        //public virtual ICollection<Menu> ChildMenus { get; set; }      
        public new static void OnModelCreating(ModelBuilder modelBuilder)
        {

            //modelBuilder.Entity<Menu>().HasMany(menu => menu.ChildMenus).WithOne(menu => menu.Parent).HasForeignKey(menu => menu.ParentId) .OnDelete(DeleteBehavior.ClientSetNull);

          
        }
    }
}
